package org.theonept.travian.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.theonept.travian.util.HttpUtil;

public class Ressource extends AbstractBatiment implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8848596967170306715L;

	private Integer production;

	Production productionEntity;

	/**
	 * @return the production
	 */
	public Integer getProduction() {
		return production;
	}

	/**
	 * @param production
	 *            the production to set
	 */
	public void setProduction(Integer production) {
		this.production = production;
	}

	public void getDetail() {

		Document documentRessourcePrecise = HttpUtil.getDocumentFromUrl(getVillage().getCompte().getUrlServeur()
				+ "build.php?id=" + getId());

		Element elementProduction = documentRessourcePrecise.getElementById("build_value")
				.getElementsByAttributeValue("class", "currentLevel").first()
				.getElementsByAttributeValue("class", "number").first();
		setProduction(Production.getInt(elementProduction.text()));

		Element contract = documentRessourcePrecise.getElementById("contract");
		setMaxLevel(!contract.getElementsByAttributeValue("class", "contractLink").isEmpty());

		Elements cost = contract.getElementsByAttributeValue("class", "showCosts");
		if (!cost.isEmpty()) {
			List<Element> elementUpProduction = cost.first().children();
			for (Element element : elementUpProduction) {
				if (element.attr("class").contains("r1")) {
					setCostBois(Production.getInt(element.text()));
				} else if (element.attr("class").contains("r2")) {
					setCostArgile(Production.getInt(element.text()));
				} else if (element.attr("class").contains("r3")) {
					setCostFer(Production.getInt(element.text()));
				} else if (element.attr("class").contains("r4")) {
					setCostCereal(Production.getInt(element.text()));
				} else if (element.attr("class").contains("r5")) {
					setConsoCereal(Production.getInt(element.text()));
				}
			}
		}
	}

	/**
	 * @return the costCereal
	 */
	public Integer getCostCereal() {
		return costCereal;
	}

	static List<Ressource> getRessources(Production production) {
		List<Ressource> ressources = new ArrayList<Ressource>();

		Document documentRessource = HttpUtil.getDocumentFromUrl(production.getVillage().getCompte().getUrlServeur()
				+ "dorf1.php?newdid=" + production.getVillage().getIdVillage());
		List<Element> elementResss = documentRessource.getElementById("village_map")
				.getElementsByAttributeValueStarting("class", "level");

		for (int i = 0; i < elementResss.size(); i++) {
			Element elementRess = elementResss.get(i);
			String[] classValue = elementRess.attr("class").split(" ");
			if (classValue[1].equals("gid1")) {
				Bois ressource = new Bois();
				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1);
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);

			} else if (classValue[1].equals("gid2")) {
				Argile ressource = new Argile();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1);
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);

			} else if (classValue[1].equals("gid3")) {
				Fer ressource = new Fer();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1);
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);

			} else if (classValue[1].equals("gid4")) {
				Cereal ressource = new Cereal();

				ressource.setVillage(production.getVillage());
				ressource.setProductionEntity(production);
				ressource.setId(i + 1);
				ressource.setLevel(elementRess.text());
				ressources.add(ressource);

			}

		}

		return ressources;
	}

	/**
	 * @return the productionEntity
	 */
	public Production getProductionEntity() {
		return productionEntity;
	}

	/**
	 * @param productionEntity
	 *            the productionEntity to set
	 */
	public void setProductionEntity(Production productionEntity) {
		this.productionEntity = productionEntity;
	}

}
